import request from '@/utils/request'
// 统一请求体
const creatRequestView = () => {
  return {
    reqInMsgHeader: {
      ReqModles: 'xxx'// 请求模式-备用
    },
    main: { // 只用于user所以固定属性名称
    }
  }
}

const defaultRequestView = creatRequestView()// 获得初始化的请求体对象
const updateDefaultRequestView = { // 创建更新默认请求实体对象方法
  update: (obj) => {
    Object.assign(defaultRequestView.main, obj) // 用法：assign(target,obj)
  },
  updateRequestHeader: (obj) => {
    Object.assign(defaultRequestView.reqInMsgHeader, obj) // 用法：assign(target,obj)
  }
}
export function getProvinces() {
  return request({
    url: '/province/getAll',
    method: 'post',
    data: defaultRequestView
  })
}
export function getDictsByType(type) {
  return request({
    url: '/dicts/getDictsByType?dictType=' + type,
    method: 'get'
  })
}
export function getTreeDictsByType(type) {
  return request({
    url: '/dicts/getTreeDictsByType?dictType=' + type,
    method: 'get'
  })
}
export function getMapTreeDictsByType(type) {
  return request({
    url: '/dicts/getMapTreeDictsByType?dictType=' + type,
    method: 'get'
  })
}
export function getDictsList(dict, code) {
  if (dict) {
    return dict.find(item => item.code === code) ? dict.find(item => item.code === code).value : ''
  } else {
    return null
  }
}
export function getParentDictsList(dict, code) {
  if (dict) {
    for (const dictElement of dict) {
      if (dictElement.children) {
        if (dictElement.children.find(item => item.code === code)) {
          return dictElement.value
        }
      }
    }
  } else {
    return null
  }
}
